package org.framework.lazy.cloud.network.heartbeat.server.config;

import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.flow.permeate.HandleChannelFlowAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.flow.proxy.HandleChannelProxyFlowAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.permeate.ServerHandlerInPermeateFlowHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.permeate.ServerHandlerOutPermeateFlowHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerHandlerInProxyFlowHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerHandlerOutProxyFlowHandler;
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyProxyFlowApplication;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyVisitorPortFlowApplication;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Role;

import java.util.List;

/**
 * @see ChannelFlowAdapter
 * @see HandleChannelFlowAdvanced
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class ServerFlowConfiguration {
    /**
     * 进口数据处理
     *
     * @return ServerHandlerInPermeateFlowHandler
     */
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public ServerHandlerInPermeateFlowHandler serverHandlerInFlowHandler(LazyVisitorPortFlowApplication lazyVisitorPortFlowApplication, ServerNodeProperties serverNodeProperties) {
        return new ServerHandlerInPermeateFlowHandler(lazyVisitorPortFlowApplication, serverNodeProperties);
    }

    /**
     * 出口数据处理
     *
     * @return ServerHandlerOutPermeateFlowHandler
     */
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public ServerHandlerOutPermeateFlowHandler serverHandlerOutFlowHandler(LazyVisitorPortFlowApplication lazyVisitorPortFlowApplication, ServerNodeProperties serverNodeProperties) {
        return new ServerHandlerOutPermeateFlowHandler(lazyVisitorPortFlowApplication, serverNodeProperties);
    }


    /**
     * 服务端流量适配器
     *
     * @param handleChannelFlowAdvancedList 服务端流量适配者
     * @return 服务端流量适配器
     */
    @ConditionalOnMissingBean(ChannelFlowAdapter.class)
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public ChannelFlowAdapter channelFlowAdapter(List<HandleChannelFlowAdvanced> handleChannelFlowAdvancedList) {
        return new ChannelFlowAdapter(handleChannelFlowAdvancedList);
    }


    /**
     * 进口数据处理
     *
     * @return serverHandlerInProxyFlowHandler
     */
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public ServerHandlerInProxyFlowHandler serverHandlerInProxyFlowHandler(LazyProxyFlowApplication lazyProxyFlowApplication, ServerNodeProperties serverNodeProperties) {
        return new ServerHandlerInProxyFlowHandler(lazyProxyFlowApplication, serverNodeProperties);
    }

    /**
     * 出口数据处理
     *
     * @return serverHandlerOutProxyFlowHandler
     */
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public ServerHandlerOutProxyFlowHandler serverHandlerOutProxyFlowHandler(LazyProxyFlowApplication lazyProxyFlowApplication, ServerNodeProperties serverNodeProperties) {
        return new ServerHandlerOutProxyFlowHandler(lazyProxyFlowApplication, serverNodeProperties);
    }


    /**
     * 服务端流量适配器
     *
     * @param handleChannelProxyFlowAdvancedList 服务端流量适配者
     * @return 服务端流量适配器
     */
    @ConditionalOnMissingBean(ChannelProxyFlowAdapter.class)
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public ChannelProxyFlowAdapter channelProxyFlowAdapter(List<HandleChannelProxyFlowAdvanced> handleChannelProxyFlowAdvancedList) {
        return new ChannelProxyFlowAdapter(handleChannelProxyFlowAdvancedList);
    }

}
